//
//  SISSFindNewHouseCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/19.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSFindNewHouseCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SISSNewHouseBuildingModel.h"

@interface SISSFindNewHouseCell ()

@property (weak, nonatomic) UIImageView *houseTypeImageView;
@property (weak, nonatomic) UILabel *housesNameLabel;
//@property (weak, nonatomic) UILabel *sellLabel;
@property (weak, nonatomic) UIImageView *sellImageView;
@property (weak, nonatomic) UILabel *middleLabel;
@property (weak, nonatomic) UILabel *propertyTypeLabel;
@property (weak, nonatomic) UIView *labelsView;
@property (strong, nonatomic) NSArray<UILabel *> *markLabels;
@property (weak, nonatomic) UILabel *bottomLabel;
@property (weak, nonatomic) UIView  *bottomLine;

@end
@implementation SISSFindNewHouseCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


- (void)setHouseBuildingModel:(SISSNewHouseBuildingModel *)houseBuildingModel {
    _houseBuildingModel = houseBuildingModel;
    
    [self.houseTypeImageView sd_setImageWithURL:[NSURL URLWithString:houseBuildingModel.buildImageUrl] placeholderImage:[UIImage imageNamed:@"图片加载占位符"]];
    self.housesNameLabel.text = houseBuildingModel.pname;
    self.middleLabel.text = [NSString stringWithFormat:@"剩余%@套 %@%@-%@",houseBuildingModel.sellCount,houseBuildingModel.town,houseBuildingModel.block,houseBuildingModel.btype];
    self.propertyTypeLabel.text = @"物业类型";
    
    for (UILabel *label in self.markLabels) {
        label.hidden = YES;
    }
    NSArray *houseTypeArr = [houseBuildingModel.type componentsSeparatedByString:@","];
    for (NSInteger i = 0; i < houseTypeArr.count ; i++) {
        UILabel *label = self.markLabels[i];
        label.hidden = NO;
        label.text = houseTypeArr[i];
        label.text = [NSString stringWithFormat:@"%@",houseTypeArr[i]];
        //        UIColor *color = [SISSPublicMethods matchTextColorWithHouseFeatureStr:houseFeatureArr[i]];
        UIColor *color = UIColor.redColor;
        label.layer.borderColor = color.CGColor;
        label.textColor = color;
    }
        [self.labelsView mas_updateConstraints:^(MASConstraintMaker *make) {
            if (houseBuildingModel.type == 0) {
                make.height.mas_equalTo(1);
            } else {
                make.height.mas_equalTo(self.markLabels[houseTypeArr.count - 1].frame.origin.y + 30);
            }
        }];
   
    NSString *unitStr = [houseBuildingModel.priceRange isEqualToString:@"未定价"] ? @"" : @"元/㎡";
    NSMutableAttributedString *mAttributedStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",houseBuildingModel.priceRange,unitStr]];
    [mAttributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,houseBuildingModel.priceRange.length)];
    self.bottomLabel.attributedText = mAttributedStr;
}


- (void)initUI {
    [super initUI];
    
    UIImageView *houseTypeImageView = [[UIImageView alloc] init];
    houseTypeImageView.image = [UIImage imageNamed:@"空白"];
    [self.contentView addSubview:houseTypeImageView];
    self.houseTypeImageView = houseTypeImageView;
    
    UILabel *housesNameLabel = [UILabel yyr_labelWithText:@"{楼盘名称}" fontSize:18 textColor:MAIN_TEXT_COLOR];
    [self.contentView addSubview:housesNameLabel];
    self.housesNameLabel = housesNameLabel;
    
//    UILabel *sellLabel = [UILabel yyr_labelWithText:@" 售 " fontSize:15 textColor:UIColor.whiteColor];
//    sellLabel.layer.cornerRadius = 4;
//    sellLabel.layer.masksToBounds = YES;
//    sellLabel.backgroundColor = COLOR(0x54B095);
//    [self.contentView addSubview:sellLabel];
//    self.sellLabel = sellLabel;
    
    UIImageView *sellImageView = [[UIImageView alloc] init];
    sellImageView.image = [UIImage imageNamed:@"售-anz"];
    [self.contentView addSubview:sellImageView];
    self.sellImageView = sellImageView;
    
    UILabel *middleLabel = [UILabel yyr_labelWithText:@"剩余{N}套 区域-商圈" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.contentView addSubview:middleLabel];
    self.middleLabel = middleLabel;
    
    UILabel *propertyTypeLabel = [UILabel yyr_labelWithText:@"物业类型" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self.contentView addSubview:propertyTypeLabel];
    self.propertyTypeLabel = propertyTypeLabel;
    
    UIView *labelsView = [[UIView alloc] init];
    [self.contentView addSubview:labelsView];
    self.labelsView = labelsView;
    
    NSMutableArray<UILabel *> *arrM = [NSMutableArray arrayWithCapacity:6];
    float labelX = 8;
    float labelY = 0;
    CGFloat labelWidth = 48;
    for (NSInteger i = 0; i < 6; i++) {
        if (labelX + labelWidth > SCREEN_WIDTH - 120 - 66) {
            labelX = 8;
            labelY += 30;
        }
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelWidth, 16)];
        label.textColor = SECONDARY_TEXT_COLOR;
        label.font = [UIFont systemFontOfSize:11];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"标签";
        label.layer.borderWidth = 1;
        label.layer.cornerRadius = 4;
        label.layer.borderColor = SECONDARY_TEXT_COLOR.CGColor;
        //        label.layer.masksToBounds = YES;
        [arrM addObject:label];
        [self.labelsView addSubview:label];
        
        labelX = CGRectGetMaxX(label.frame) + 10;
//        label.hidden = YES;
        
    }
    
    self.markLabels = arrM;
    
    UILabel *bottomLabel = [UILabel yyr_labelWithText:@"{售价区间}元/㎡" fontSize:15 textColor:MAIN_TEXT_COLOR ];
    [self.contentView addSubview:bottomLabel];
    self.bottomLabel = bottomLabel;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGSize imageSize = CGSizeMake(110, 110);
    CGSize sellImageSize = CGSizeMake(21, 21);
    [self.houseTypeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.contentView).offset(2 * globelMargin);
        make.size.mas_equalTo(imageSize);
    }];
    
    [self.housesNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.houseTypeImageView.mas_right).offset(globelMargin);
        make.top.equalTo(weakSelf.houseTypeImageView);
    }];
    
//    [self.sellLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(weakSelf.housesNameLabel);
//        make.top.equalTo(weakSelf.housesNameLabel.mas_bottom).offset(globelMargin);
//        make.height.mas_equalTo(3 * globelMargin);
//        make.width.mas_equalTo(3 * globelMargin);
//    }];
    
    [self.sellImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.housesNameLabel);
        make.top.equalTo(weakSelf.housesNameLabel.mas_bottom).offset(globelMargin);
        make.size.mas_equalTo(sellImageSize);
    }];
    
    [self.middleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.sellImageView.mas_right).offset(0.5 * globelMargin);
        make.centerY.equalTo(weakSelf.sellImageView);
        make.right.equalTo(weakSelf.contentView).offset(- globelMargin);
    }];
    
    [self.propertyTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.sellImageView);
        make.top.equalTo(weakSelf.sellImageView.mas_bottom).offset(globelMargin);
    }];
    
    [self.labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.propertyTypeLabel.mas_right);
        make.top.equalTo(weakSelf.propertyTypeLabel);
        make.right.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(20);
    }];
    
    [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.propertyTypeLabel);
        make.top.equalTo(weakSelf.labelsView.mas_bottom);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.bottomLabel.mas_bottom).offset(globelMargin);
        make.left.bottom.right.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
